<?php
/*
Plugin Name: Deezer In WP
Plugin URI: http://easyaswebsite.free.fr/
Description: Create XHTML code to embed deezer player in your articles [diwp:<em>KeyIDSong(or path+id)</em> (optionnal:<em>auto=1</em> for autoplay)] and in your sidebar as widget !
Version: 1.3
Author: Netsson
Author URI: http://easyaswebsite.free.fr/
*/
$diwp_version = "1.3";
$diwp_userlevel = 8;
$count=0;

define('DIWP_MAX', 15);

if ( !function_exists('current_user_can') ) :
	function current_user_can() { return 0; }
endif;

if(function_exists('load_plugin_textdomain')) load_plugin_textdomain('diwp', false, dirname(plugin_basename(__FILE__)));

function diwp_embed($matches) {
global $count;

	$PathToPlugin = get_bloginfo('wpurl') . "/wp-content/plugins/diwp";
	
	$stringstart = $matches[3];
	
	$widthdefine = 220;
	$autoplaydefine = 0;
	
	if(strstr($stringstart," ")){
		if(strstr($stringstart,'auto=')){
			$pos_autoplay = strpos($stringstart, 'auto=', 0);
			$autoplaydefine = substr($stringstart, $pos_autoplay+5, 1);
		}
		if(strstr($stringstart,'player=')){
			$pos_player = strpos($stringstart, 'player=', 0);
			$playerdefine = substr($stringstart, $pos_player+7);
		}
		if(strstr($stringstart,'backcolor=')){
			$pos_backc = strpos($stringstart, 'backcolor=', 0);
			$backcdefine = substr($stringstart, $pos_backc+10, 6);
		}
		if(strstr($stringstart,'textcolor=')){
			$pos_textc = strpos($stringstart, 'textcolor=', 0);
			$textcdefine = substr($stringstart, $pos_textc+10, 6);
		}
		$pos_endidsong = strpos($stringstart, ' ', 0);
		$idsongdefine = substr($stringstart, 0, $pos_endidsong);
	}else
		$idsongdefine = $stringstart;

	$idsong = $idsongdefine;
	
	if(strstr($idsong,'+')){ $explode_idsong=explode('+',$idsong); $idsong=$explode_idsong[0]; $id=$explode_idsong[1]; }
	
	//Variables principales
	$autoplay=$autoplaydefine;
	//Variables design
	$colorVolume=get_option('diwp_volumecolor') ? get_option('diwp_volumecolor') : 'ffffff';
	if(!isset($textcdefine))$textColor1=get_option('diwp_textcolor') ? get_option('diwp_textcolor') : 'ffffff'; else $textColor1=$textcdefine;
	if(!isset($backcdefine))$colorBackground=get_option('diwp_bgcolor') ? get_option('diwp_bgcolor') : 'ff0000'; else $colorBackground=$backcdefine;
	$colorButtons=get_option('diwp_bucolor') ? get_option('diwp_bucolor') : 'BF3926';
	if(!isset($playerdefine) || $playerdefine=='widget.swf') $player='small-widget-v2.swf'; else $player=$playerdefine;
	if(strstr($idsongdefine,'+')) $player='widget.swf';
	
	$pathoridsong = (strstr($idsongdefine,'+')) ? 'id='.@$id.'&amp;path' : 'idSong';
	
	if(strstr($idsongdefine,'+')){
		$widthandheight='width="180" height="215"';
		$paramname='movie';
		$backcolor='colorBack';
		$textcolor='colorText';
		$other='&amp;lang='.strtoupper(WP_LANG);
	}elseif($playerdefine=='small-widget.swf'){
		$widthandheight='width="180" height="25"';
		$paramname='src';
		$backcolor='colorBackground';
		$textcolor='textColor1';
		$other='';
	}else{
		$widthandheight='width="220" height="55"';
		$paramname='src';
		$backcolor='colorBackground';
		$textcolor='textColor1';
		$other='';
	}
	
	$input='<object '.$widthandheight.' data="http://www.deezer.com/embedded/'.$player;
	$input.='?'.$pathoridsong.'='.$idsong.'&amp;'.$backcolor.'=0x'.$colorBackground;
	$input.='&amp;'.$textcolor.'=0x'.$textColor1.'&amp;colorButtons=0x'.$colorButtons.'&amp;colorVolume=0x'.$colorVolume;
	$input.='&amp;autoplay='.$autoplay.'" type="application/x-shockwave-flash" id="diwp_'.$count.'">';
	$input.='<param name="'.$paramname.'" value="http://www.deezer.com/embedded/'.$player.'?'.$pathoridsong.'='.$idsong;
	$input.='&amp;'.$backcolor.'=0x'.$colorBackground.'&amp;'.$textcolor.'=0x'.$textColor1;
	$input.='&amp;colorButtons=0x'.$colorButtons.'&amp;colorVolume=0x'.$colorVolume.'&amp;autoplay='.$autoplay.'" />';
	$input.='</object>';
	
$count+=1;
return $input;
}

function diwp_parse($content) {
	$allblocks = '(?:address|blockquote|code|div|h[1-6]|p|pre)';
	$content = preg_replace_callback( "/(<$allblocks(.*?)>)?\[diwp:([^]]+)](<\/$allblocks>)?/i", "diwp_embed", $content );
	return $content;
}

function diwp_admin_page () { 
global $user_level, $diwp_userlevel, $diwp_version;

    get_currentuserinfo();

	$current=current_user_can('level_'.$diwp_userlevel);
	
	$PathToPlugin = get_bloginfo('wpurl') . "/wp-content/plugins/diwp";
	
	echo '<script src="'.$PathToPlugin.'/colorpicker.js" type="text/javascript"></script>';
	
	if ($user_level < $diwp_userlevel &&  $current!=1) {
		echo '<div class="wrap"><h2>' . __("No Access for you!",'diwp') .'</h2></div>';
	}
	else { ?>
	<div class="wrap">
		<h2><?php _e('DIWP Player','diwp');?> v. <?php echo $diwp_version; ?></h2>
		<?php
		//Variables design
		$colorVolume=get_option('diwp_volumecolor') ? get_option('diwp_volumecolor') : 'ffffff';
		$textColor1=get_option('diwp_textcolor') ? get_option('diwp_textcolor') : 'ffffff';
		$colorBackground=get_option('diwp_bgcolor') ? get_option('diwp_bgcolor') : 'ff0000';
		$colorButtons=get_option('diwp_bucolor') ? get_option('diwp_bucolor') : 'BF3926';
		?>

		<form name="diwp_options" action="edit.php?page=diwp" method="get" id="diwp_options">
		<input type="hidden" name="page" value="diwp" />
        <div style="float:left; width:405px; height:100%; border:#000 1px solid; padding:3px 3px 3px 3px">
		<fieldset>
			<legend><b><?php _e('Default Colors (Must be 6 digit hex)','diwp');?></b></legend>
            <br/>
			<div class="UserOption">
				<?php _e('Background color','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp_bgcolor" value="<?php echo $colorBackground; ?>" size="6" /><br />
				
                <?php _e('Text color','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp_textcolor" value="<?php echo $textColor1; ?>" size="6" /><br />
                
                <?php _e('Volume color (only for v2 and playlist player)','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp_volumecolor" value="<?php echo $colorVolume; ?>" size="6" /><br />
                
                <?php _e('Buttons color (only for v1 player)','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp_colorButtons" value="<?php echo $colorButtons; ?>" size="6" /><br />
			</div>
		</fieldset>
        <hr/>
        <br/>
		<div style="text-align:center"><input type="submit" name="diwp_options" value="<?php _e('Save Parameters','diwp');?>" class="button" style="font-size: 140%"  /></div>
        <br/><hr/><br/>
            <div style="float:left; margin-right:2px;">
            <?php _e('Playlist Player Generator','diwp');?><br/><br/>
            <?php
			$inputadmin1='<object width="180" height="215" data="http://www.deezer.com/embedded/widget.swf';
			$inputadmin1.='?path=6793467&amp;id=1673591&amp;colorBack=0x'.$colorBackground;
			$inputadmin1.='&amp;colorText=0x'.$textColor1.'&amp;colorVolume=0x'.$colorVolume;
			$inputadmin1.='&amp;autoplay=0" type="application/x-shockwave-flash" id="diwp_pl_1">';
			$inputadmin1.='<param name="movie" value="http://www.deezer.com/embedded/widget.swf?path=6793467&amp;id=1673591';
			$inputadmin1.='&amp;colorBack=0x'.$colorBackground.'&amp;colorText=0x'.$textColor1;
			$inputadmin1.='&amp;colorVolume=0x'.$colorVolume.'&amp;autoplay=0" />';
			$inputadmin1.='</object>';
			echo $inputadmin1; 
			?>
            </div>
            <div style="float:left; margin-bottom:10px;">
        	<?php _e('V2 Player Generator','diwp');?><br/><br/>
            <?php
			$inputadmin='<object width="220" height="55" data="http://www.deezer.com/embedded/small-widget-v2.swf';
			$inputadmin.='?idSong=585037&amp;colorBackground=0x'.$colorBackground;
			$inputadmin.='&amp;textColor1=0x'.$textColor1.'&amp;colorVolume=0x'.$colorVolume;
			$inputadmin.='&amp;autoplay=0" type="application/x-shockwave-flash" id="diwp_v2_1">';
			$inputadmin.='<param name="src" value="http://www.deezer.com/embedded/'.$player.'?idSong=585037';
			$inputadmin.='&amp;colorBackground=0x'.$colorBackground.'&amp;textColor1=0x'.$textColor1;
			$inputadmin.='&amp;colorVolume=0x'.$colorVolume.'&amp;autoplay=0" />';
			$inputadmin.='</object>';
            echo $inputadmin; 
			?>
            </div>
            <?php _e('V1 Player Generator','diwp');?><br/><br/>
            <?php
			$inputadmin2='<object width="180" height="25" data="http://www.deezer.com/embedded/small-widget.swf';
			$inputadmin2.='?idSong=585037&amp;colorButtons=0x'.$colorButtons.'&amp;colorBackground=0x'.$colorBackground;
			$inputadmin2.='&amp;textColor1=0x'.$textColor1;
			$inputadmin2.='&amp;autoplay=0" type="application/x-shockwave-flash" id="diwp_v1_1">';
			$inputadmin2.='<param name="movie" value="http://www.deezer.com/embedded/widget.swf?idSong=585037';
			$inputadmin2.='&amp;colorBack=0x'.$colorBackground.'&amp;colorText=0x'.$textColor1;
			$inputadmin2.='&amp;autoplay=0" />';
			$inputadmin2.='</object>';
			echo $inputadmin2;
			?>
        </div>
        <div style="float:left; width:25%; border:#000 1px solid; padding:5px 5px 5px 5px; margin-left:10px">
           <?php _e('Color Picker','diwp');?><br/><br/>
            <input type="text" class="color" value="#FFFF00">
            <img src="<?php echo $PathToPlugin; ?>/images/barre.png" />
            <div style="text-align:center"><?php _e('Copy this value into the field<br/> you want to change color','diwp')?></div>
            <br/>
        </div>
		</form>
	</div>
<?php }
}

function diwp_options() {
global $user_level, $diwp_userlevel;

    get_currentuserinfo();
	$current=current_user_can('level_'.$diwp_userlevel);

    if ($user_level <  $diwp_userlevel && $current!=1) die(__("Cheatin' uh ?"));

	update_option('diwp_bgcolor', $_GET['diwp_bgcolor']);
	update_option('diwp_textcolor', $_GET['diwp_textcolor']);
	update_option('diwp_volumecolor', $_GET['diwp_volumecolor']);
	update_option('diwp_bucolor', $_GET['diwp_colorButtons']);
}

function diwp_add_admin_page() {
global $diwp_userlevel;
add_management_page('DIWP Player Parameters', __('DIWP Player','diwp'), $diwp_userlevel, 'diwp', 'diwp_admin_page');
}


function wp_widget_diwp( $args, $number = 1 ) {
global $buttonwidth_default, $sliderwidth_default, $sliderheight_default, $volumewidth_default, $volumeheight_default;
	extract($args);
	$options = get_option('widget_diwp');
	
	$title=$options[$number]['title'] ? $options[$number]['title'] : 'DIWP Player '.$number;
	$idsongdefine=$options[$number]['idsong'];
	$autoplay=$options[$number]['auto'] ? $options[$number]['auto'] : '0';
	$player=strstr($idsongdefine,'+') ? 'widget.swf' : 'small-widget.swf';
	$colorVolume=$options[$number]['volumecolor'] ? $options[$number]['volumecolor'] : '555555';
	$colorBackground=$options[$number]['bgcolor'] ? $options[$number]['bgcolor'] : 'ff0000';
	$colorButtons=$options[$number]['buttoncolor'] ? $options[$number]['buttoncolor'] : '888888';
	$textColor1=$options[$number]['textcolor'] ? $options[$number]['textcolor'] : 'ffffff';
	
	if(strstr($idsongdefine,'+')){ $explode_idsong=explode('+',$idsongdefine); $idsong=$explode_idsong[0]; $id=$explode_idsong[1]; }
	else $idsong=$idsongdefine;
	
	$pathoridsong = (strstr($idsongdefine,'+')) ? 'id='.@$id.'&amp;path' : 'idSong';

	if(strstr($idsongdefine,'+')){
		$widthandheight='width="180" height="215"';
		$paramname='movie';
		$backcolor='colorBack';
		$textcolor='colorText';
		$other='&amp;lang='.strtoupper(WP_LANG);
	}else{
		$widthandheight='width="180" height="25"';
		$paramname='src';
		$backcolor='colorBackground';
		$textcolor='textColor1';
		$other='';
	}
	
	$input='<object '.$widthandheight.' data="http://www.deezer.com/embedded/'.$player;
	$input.='?'.$pathoridsong.'='.$idsong.'&amp;'.$backcolor.'=0x'.$colorBackground;
	$input.='&amp;'.$textcolor.'=0x'.$textColor1.'&amp;colorButtons=0x'.$colorButtons.'&amp;colorVolume=0x'.$colorVolume;
	$input.='&amp;autoplay='.$autoplay.'" type="application/x-shockwave-flash" id="diwp_w_'.$number.'">';
	$input.='<param name="'.$paramname.'" value="http://www.deezer.com/embedded/'.$player.'?'.$pathoridsong.'='.$idsong;
	$input.='&amp;'.$backcolor.'=0x'.$colorBackground.'&amp;'.$textcolor.'=0x'.$textColor1;
	$input.='&amp;colorButtons=0x'.$colorButtons.'&amp;colorVolume=0x'.$colorVolume.'&amp;autoplay='.$autoplay.'" />';
	$input.='</object>';
	
	echo $before_widget;
	echo $before_title . $title . $after_title;
	echo $input;
	echo $after_widget;
}

function wp_widget_diwp_control($number) {
	$options = $newoptions = get_option('widget_diwp');
	$newoptions = $options;
	if ( $_POST["diwp-submit-$number"] ) {
		$newoptions[$number]['title'] = strip_tags(stripslashes($_POST["diwp-title-$number"]));
		$newoptions[$number]['auto'] = strip_tags(stripslashes($_POST["diwp-auto-$number"]));
		$newoptions[$number]['idsong'] = strip_tags(stripslashes($_POST["diwp-idsong-$number"]));
		$newoptions[$number]['volumecolor'] = strip_tags(stripslashes($_POST["diwp-volumecolor-$number"]));
		$newoptions[$number]['bgcolor'] = strip_tags(stripslashes($_POST["diwp-bgcolor-$number"]));
		$newoptions[$number]['buttoncolor'] = strip_tags(stripslashes($_POST["diwp-buttoncolor-$number"]));
		$newoptions[$number]['textcolor'] = strip_tags(stripslashes($_POST["diwp-textcolor-$number"]));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_diwp', $options);
	}
	
	$title=$options[$number]['title'] ? $options[$number]['title'] : 'DIWP Player '.$number;
	$idsong=$options[$number]['idsong'];
	$auto=$options[$number]['auto'] ? $options[$number]['auto'] : '0';
	$colorVolume=$options[$number]['volumecolor'] ? $options[$number]['volumecolor'] : 'ffffff';
	$textColor1=$options[$number]['textcolor'] ? $options[$number]['textcolor'] : 'ffffff';
	$colorBackground=$options[$number]['bgcolor'] ? $options[$number]['bgcolor'] : 'FF0000';
	$colorButtons=$options[$number]['bucolor'] ? $options[$number]['bucolor'] : 'BF3926';?>
    
    <fieldset style="border-bottom:#000 1px dotted">
		<legend><b><?php _e('General Parameters','diwp');?></b></legend>
        <br/>
			<div class="UserOption">
            	<?php _e('Title','diwp');?> : 
				<input type="text" name="diwp-title-<?php echo $number; ?>" value="<?php echo $title; ?>" size="40" /><br />
                
            	<?php _e('<strong>Key ID Song</strong> or <strong>Path+ID of Playlist</strong>','diwp');?> : 
				<input type="text" name="diwp-idsong-<?php echo $number; ?>" value="<?php echo $idsong; ?>" size="16" /><br />
                
                <input <?php if($auto == '1')echo'checked';?> name="diwp-auto-<?php echo $number; ?>" type="checkbox" value="1"> <?php _e('Autoplay','diwp');?><br/><br/>
			</div>
		</fieldset>
		<fieldset>
			<legend><br/><b><?php _e('Colors (Must be 6 digit hex for colors)','diwp');?></b></legend>
            <br/>
			<div class="UserOption">
				<?php _e('Background color','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp-bgcolor-<?php echo $number; ?>" value="<?php echo $colorBackground; ?>" size="6" /><br/>
                
                <?php _e('Text color','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp-textcolor-<?php echo $number; ?>" value="<?php echo $textColor1; ?>" size="6" /><br />
                
                <?php _e('Volume color (only for playlist player)','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp-volumecolor-<?php echo $number; ?>" value="<?php echo $colorVolume; ?>" size="6" /><br />
                
                <?php _e('Buttons color (only for small player)','diwp');?> : 
				#<input type="text" maxlength="6" name="diwp-buttoncolor-<?php echo $number; ?>" value="<?php echo $colorButtons; ?>" size="6" /><br/>
			</div>
		</fieldset>
        <br/><hr/><br/>
            <div style="float:left; margin-right:2px;">
            <?php _e('Playlist Player Generator','diwp');?><br/><br/>
            <?php
			$inputadmin1='<object width="180" height="215" data="http://www.deezer.com/embedded/widget.swf';
			$inputadmin1.='?path=6793467&amp;id=1673591&amp;colorBack=0x'.$colorBackground;
			$inputadmin1.='&amp;colorText=0x'.$textColor1.'&amp;colorVolume=0x'.$colorVolume;
			$inputadmin1.='&amp;autoplay=0" type="application/x-shockwave-flash" id="diwp_pl_1">';
			$inputadmin1.='<param name="movie" value="http://www.deezer.com/embedded/widget.swf?path=6793467&amp;id=1673591';
			$inputadmin1.='&amp;colorBack=0x'.$colorBackground.'&amp;colorText=0x'.$textColor1;
			$inputadmin1.='&amp;colorVolume=0x'.$colorVolume.'&amp;autoplay=0" />';
			$inputadmin1.='</object>';
			echo $inputadmin1; 
			?>
            </div>
            <div style="float:left; margin-bottom:10px;">
            <?php _e('Small Player Generator','diwp');?><br/><br/>
            <?php
			$inputadmin2='<object width="180" height="25" data="http://www.deezer.com/embedded/small-widget.swf';
			$inputadmin2.='?idSong=585037&amp;colorButtons=0x'.$colorButtons.'&amp;colorBackground=0x'.$colorBackground;
			$inputadmin2.='&amp;textColor1=0x'.$textColor1;
			$inputadmin2.='&amp;autoplay=0" type="application/x-shockwave-flash" id="diwp_v1_1">';
			$inputadmin2.='<param name="movie" value="http://www.deezer.com/embedded/widget.swf?idSong=585037';
			$inputadmin2.='&amp;colorBack=0x'.$colorBackground.'&amp;colorText=0x'.$textColor1;
			$inputadmin2.='&amp;autoplay=0" />';
			$inputadmin2.='</object>';
			echo $inputadmin2;
			?>
            </div>
            <br/><br/>
            <div style="float:left; width:375px; text-align:center">
            <br/>
            <hr/>
            <br/>
        	<input type="hidden" id="diwp-submit-<?php echo $number; ?>" name="diwp-submit-<?php echo $number; ?>" value="1" />
       		</div>
            <fieldset>
            </fieldset>
	<?php
}

function widget_diwp_setup() {
	$options = $newoptions = get_option('widget_diwp');
	if ( isset($_POST['diwp-number-submit']) ) {
		$number = (int) $_POST['diwp-number'];
		if ( $number > DIWP_MAX ) $number = DIWP_MAX;
		if ( $number < 1 ) $number = 1;
		$newoptions['number'] = $number;
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_diwp', $options);
		widget_diwp_register($options['number']);
	}
}

function widget_diwp_page() {
	$options = $newoptions = get_option('widget_diwp'); ?>
	<div class="wrap">
		<form method="POST">
			<h2>DIWP Player Widgets</h2>
			<p style="line-height: 30px;"><?php _e('How many DIWP Player widgets would you like ?','diwp'); ?>
			<select id="diwp-number" name="diwp-number" value="<?php echo $options['number']; ?>">
				<?php for ( $i = 1; $i <= DIWP_MAX; ++$i ) 
				echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
			</select>
			<span class="submit"><input type="submit" name="diwp-number-submit" id="diwp-number-submit" 
			value="<?php _e('Save','diwp'); ?>" /></span></p>
		</form>
	</div>
<?php
}

function widget_diwp_register() {
	global $wp_version;
	$options = get_option('widget_diwp');
	$number = $options['number'];
	if ($number < 1) $number = 1;
	if ($number > DIWP_MAX) $number = DIWP_MAX;
	for ($i = 1; $i <= DIWP_MAX; $i++) {
		$name = array('DIWP Player %s', null, $i);
		if (function_exists('wp_register_sidebar_widget')){
			$id = "diwp-w-$i";
			$dims = array('width' => 380, 'height' => 300);
			$class = array('classname' => 'wp_widget_diwp');
			$name = sprintf('DIWP Player %d', $i);
			wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_diwp' : '', $class, $i);
			wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_diwp_control' : '', $dims, $i);
		}
		else{
			register_sidebar_widget($name, $i <= $number ? 'wp_widget_diwp' : '', $i);
			register_widget_control($name, $i <= $number ? 'wp_widget_diwp_control' : '', 520, 300, $i);
		}
	}
	add_action('sidebar_admin_setup', 'widget_diwp_setup');
	add_action('sidebar_admin_page', 'widget_diwp_page');
}

function diwp_on_plugins_loaded() {
	widget_diwp_register();
}


if (function_exists('add_action')) {
	add_action('admin_menu', 'diwp_add_admin_page');
	add_action('plugins_loaded', 'diwp_on_plugins_loaded');
}

if (isset($_GET['diwp_options']))
    add_action('init', 'diwp_options');

add_filter('the_content', 'diwp_parse');
?>